<?php
namespace app\admin\controller;

use app\common\model\Gameplog   as GamePayLogModel;
use app\common\model\Gamelist   as GameListModel;
use app\common\model\Player as PlayerModel;
use app\common\controller\AdminBase;
use think\Db;
use app\api\controller\GameNotify;

/**
 * 玩家管理
 * Class AdminUser
 * @package app\admin\controller
 */
class Player extends AdminBase
{
    
    protected function _initialize()
    {
        parent::_initialize();
    }
    
    
    /**
     * 玩家登陆记录
     * @param string $keyword
     * @param $page
     * @return mixed
     */
    public function llog($keyword = '', $page = 1)
    {
        $data = input();
        $map  = [];
        if(!empty($data['game'])){
        $map[]   = ['tab_gameulog.gid','=' ,$data['game']];
        }else{
            $data['game']   = null;
        }
        if(!empty($data['area'])){
            $map[]  = ['tab_gameulog.id','=',$data['area']];
        }
        if(!empty($data['ip'])){
            $map[]  = ['tab_gameulog.login_ip','=',$data['ip']];
        }
        if (!empty($data['starttime']) && !empty($data['endtime'])) {
            $map[]= ['tab_gameulog.login_time','between time',[strtotime($data['starttime']),strtotime($data['endtime'])]];
        }elseif (!empty($data['starttime'])) {
            $map[] = ['tab_gameulog.login_time','>=',strtotime($data['starttime'])];
        }elseif (!empty($data['endtime'])) {
            $map[] = ['tab_gameulog.login_time','<=',strtotime($data['endtime'])];
        }else{
            // 默认查询近一个月
            $starttime = strtotime('-1 month');
            $map[] = ['tab_gameulog.login_time','>=',$starttime];
        }
        if(isset($data['cid']) && input('cid') != ""){
            $map[]   =['tab_gameulog.cid','=',(int)input('cid')];
        }
        $field = "tab_gameulog.*,tab_gamelist.name,tab_gamearea.sname,tab_player.account,tab_player.status,tab_channel.platform";
        $game_list = Db::connect('db_config_aiqi')->name('gameulog')
         ->field($field)
         ->where($map)
         ->join('tab_gamelist','tab_gamelist.id = tab_gameulog.gid','left')
         ->join('tab_gamearea','tab_gamearea.id = tab_gameulog.sid','left')
         ->join('tab_player','tab_player.id = tab_gameulog.uid','left')
         ->join('tab_channel','tab_channel.id = tab_gameulog.cid','left')
         ->order('tab_gameulog.id desc')
         ->paginate(20);
         return $this->fetch('ulogin', ['game_list' => $game_list,'keyword' => $keyword,'data'=>$data,'reg_list' => $game_list]);
        
    }
    
    /**
     * 账号封停
     * @param $id
     */
    public function check()
    {
        if ($this->request->isPost()) {
            $id = input('id');
            $type = input('type');
            if(PlayerModel::where(array('id'=>$id))->find()){
                if (PlayerModel::where(array('id'=>$id))->update(array('status'=>$type))) {
                    $this->success('更改成功');
                } else {
                    $this->error('更改成功');
                }
            }
        }
    }
    
    public function getgarea($gid)
    {
        
        $game_list =DB::name('gamearea')->where('gid',$gid)->field('id,sname')->order('id DESC')->select();
        return json($game_list);
    }
    
    
    /**
     * 玩家管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function logininfo()
    {
        /* 查询条件初始化 */
        $data = input();
        $page = $data['page'] ?? 1;
        $map  = [];
        $game_list =[];
        if(!empty($data['ip'])){
            $map[]  = ['login_ip','=',$data['ip']];
        }
        if (!empty($data['starttime']) && !empty($data['endtime'])) {
            $map[]= ['login_time','between time',[strtotime($data['starttime']),strtotime($data['endtime'])]];
        }elseif (!empty($data['starttime'])) {
            $map[] = ['login_time','>=',strtotime($data['starttime'])];
        }elseif (!empty($data['endtime'])) {
            $map[] = ['login_time','<=',strtotime($data['endtime'])];
        }
        if(!empty($data['game'])){
            $map[]   =['gid','=',(int)input('game')];
        }else{
            $data['game']   = null;
        }
        if(isset($data['cid']) && input('cid') != ""){
            $map[]   =['cid','=',(int)input('cid')];
        }else{
            $data['cid']   = null;
        }
        if(!empty($data['account'])){
            $map[]   =['account','like','%'.$data['account'].'%'];
        }
        $game_list = PlayerModel::where($map)->order("id desc")
        ->paginate(20, false, ['page' => $page,'query' =>request()->param()]);
        return $this->fetch('logininfo', ['game_list' => $game_list,'reg_list' => $game_list,'data' => $data]);
    }


    /**
     * 旧版sdk账号管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function jinling()
    {
        /* 查询条件初始化 */
        $data = input();
        $page = $data['page'] ?? 1;
        $map  = [];
        if(!empty($data['gd_id'])){
            $map[]   =['gd_id','like','%'.$data['gd_id'].'%'];
        }
        //tab_jinling_palyer

        $game_list = DB::connect('db_config_aiqi')->name('jinling_palyer')->where($map)
            ->order("id desc")
            ->paginate(20, false, ['page' => $page,'query' =>request()->param()]);
        return $this->fetch('jinling', ['game_list' => $game_list,'reg_list' => $game_list]);
    }

    /**
     * @param $id
     * @return void
     */
    public function jingling_del($id = 0)
    {
        if ($id) {
            if (DB::connect('db_config_aiqi')->name('jinling_palyer')->where(['id'=>$id])->delete()) {
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }

        } else {
            $this->error('请选择需要账号');
        }
    }
    
    /**
     * 充值记录
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function plog($page = 1)
    {
		/* 查询条件初始化 */
    	$data = input();
        $map  = [];
		$game_list =[];
        if(!empty($data['keyword'])){
		$map[]  = ['a.uid|a.porder','like', '%'.$data['keyword'].'%'];
        }
        if(!empty($data['game'])){
            $map[]   = ['a.gid','=' ,$data['game']];
        }else{
		    $data['game']   = null;
		}
        if(!empty($data['area'])){
            $map[]  = ['a.sid','=',$data['area']];
        }
        
        if(!empty($data['status'])){
            $map[]  = ['a.pay_status','=',$data['status']-1];
        }else{
           $data['status']    = null;
        }
        if (!empty($data['starttime']) && !empty($data['endtime'])) {
            $map[]= ['a.pay_time','between time',[$data['starttime'],$data['endtime']]];
        }elseif (!empty($data['starttime'])) {
            $map[] = ['a.pay_time','>=',$data['starttime']];
        }elseif (!empty($data['endtime'])) {
            $map[] = ['a.pay_time','<=',$data['endtime']];
        }
		$field = 'a.*,b.name,c.sname,d.username as aname,d.nickname';
		//$pay_list = GamePModel::alias('a')->join('Gamelist b','b.id = a.gid','left')->join('Gamearea c','c.id = a.sid','left')->where($map)->field($field)->order('a.id DESC')->paginate(15, false, ['page' => $page,'query' =>request()->param()]);
		$pay_list = GamePayLogModel::alias('a')
            ->join('Gamelist b','b.id = a.gid','left')
            ->join('Gamearea c','c.sid = a.sid','left')
            ->join('Channel d','d.id = a.aid','left')
            ->where($map)->field($field)
            ->order('a.id DESC')
            ->paginate(15, false, ['page' => $page,'query' =>request()->param()]);
		//$pay_list = '';
		$game_list = GameListModel::order('id DESC')->select();
        return $this->fetch('pay', ['game_list' => $game_list,'pay_list' => $pay_list,'data' => $data]);
    }
    
    
    /**
     * 充值订单处理
     * @param $id
     */
    public function handleorder()
    {
        if ($this->request->isPost()) {
            $order_no = $this->request->post('order_no');
            $type = $this->request->post('type');
            if ($type == "fill"){
                $game_notify = new GameNotify();
                $result = $game_notify->game_notify($order_no);
                if($result['status']){
                    $this->success($result['message']);
                }
                $this->error($result['message']);
            }
        }
    }
    
    /*玩家充值排行*/
    /*参数  gid*/
    public function payrank($page = 1)
    {
        /* 查询条件初始化 */
        $data = input();
        $map  = [];
        $game_list =[];
        if(!empty($data['keyword'])){
            $map['a.uname|c.username']  = ['like', '%'.(string)input('keyword').'%'];
        }
        
        if(!empty($data['game'])){
            $map['a.gid']   = (int)input('game');
        }else{
            $data['game']   = null;
        }
        
        if (!empty($data['starttime']) && !empty($data['endtime'])) {
            $map['a.paytime']= ['between time',$data['starttime'],$data['endtime']];
        }elseif (!empty($data['starttime'])) {
            $map['a.paytime'] = ['>=',$data['starttime']];
        }elseif (!empty($data['endtime'])) {
            $map['a.paytime'] = ['<=',$data['endtime']];
        }
        $map['a.status']=['>=',1];
        $field = 'a.uname,b.gname,c.username as aname,sum(a.pamount) as paytotal,max(a.pamount) as maxpay,min(a.pamount) as minpay';
        $pay_rank = $this->ppay_model->alias('a')->join('GameList b','b.id = a.gid','left')->join('User c','c.id = a.agent','left')->where($map)->field($field)->group('a.uname')->order('paytotal DESC')->paginate(15, false, ['page' => $page,'query' =>request()->param()]);
        $game_list = $this->glist_model->order('id DESC')->select();
        return $this->fetch('payrank', ['game_list' => $game_list,'pay_rank' => $pay_rank,'data' => $data]);
    }


    /**
     * 查询玩家实名认证信息
     * @return array
     */
    public function auth()
    {
        $data = [];
        if(!empty(input('account'))){
            $page = input('page') ?? 1;
            $account  = input('account');
            $map[]   =['p.account','like','%'.$account.'%'];
            $field = 'p.id,p.account, a.real_name, a.id_card';
            $data  = PlayerModel::alias("p")->join('real_auth a', 'a.uid = p.id','left')
                ->where($map)->field($field)
                ->order("p.id desc")
                ->paginate(50, false, ['page' => $page,'query' =>request()->param()]);
        }
        return $this->fetch('auth',['data'=>$data,'reg_list' => $data]);
    }
}